import { toxicityProbability } from 'modules/api/perspective';
import { logError } from 'utils/log';

/**
 * Analyze every message with Perspective if this feature is enabled.
 * Store the user's scores alongside message ids.
 *
 * @param { import('telegraf/typings/context').TelegrafContext } ctx
 */
export const track = async (ctx) => {
  // TODO: abort if this feature is not enabled in the group
  const { message } = ctx;
  const {
    message_id: messageId,
    chat: { id: chatId },
  } = message;

  const result = await toxicityProbability(message.text, message.chat.id);

  if (result.error) logError(`Perspective API request failed: `, result.error);

  // TODO: store chatId, messageId, and result.score
  return console.log(
    `${messageId} in ${chatId} is ${result.score}% likely to be toxic.`,
  );
};
